<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
<div id="app">
<div class="demo-input-size">
    <el-input
            size="medium"
            placeholder="请输入菜单名"
            suffix-icon="el-icon-date"
            style="display: inline-block;width: 200px;"
            v-model="searchInfo.name"
    >
    </el-input>
    <el-button icon="el-icon-search" type="success" @click="searchMenu()">搜索菜单</el-button>
    <el-button icon="el-icon-circle-plus-outline" type="warning" @click="showAdd()">添加菜单</el-button>
    <el-table
            :data="menuArr"
            border
            style="width: 100%">
        <el-table-column
                prop="id"
                label="编号"
                width="80">
        </el-table-column>
        <el-table-column
                prop="mid"
                label="关联编号">
        </el-table-column>
        <el-table-column
                prop="name"
                label="菜单名称"
        >
        </el-table-column>
        <el-table-column
                prop="url"
                label="请求地址"
        >
        </el-table-column>
        <el-table-column
                prop="state"
                label="状态">
            <template slot-scope="scope">
                <span v-if="scope.row.state== 0">
                     <div class="state-icon icon0" style="color: #409EFF;font-weight: bolder">启动</div>
                </span>
                <span v-if="scope.row.state== 1">
                     <div class="state-icon icon1" style="color: red;font-weight: bolder">禁用</div>
                </span>
                <span v-if="scope.row.state== 2">
                     <div class="state-icon icon2" style="color: blueviolet;font-weight: bolder">冻结</div>
                </span>
            </template>
        </el-table-column>
        <el-table-column
                prop="icon"
                label="图标链接"
        >
        </el-table-column>
        <el-table-column
                prop="other"
                label="菜单类型"
        >
        </el-table-column>
        <el-table-column
                prop="type"
                label="字母类型"
        >
        </el-table-column>
        <el-table-column
                fixed="right"
                label="操作"
        >
            <template slot-scope="scope">
                <el-button icon="el-icon-delete-solid" type="warning" circle @click="deleteMenu(scope.row.id)"></el-button>
                <el-button icon="el-icon-s-tools" type="primary" circle @click="showMenu(scope.row.id)"></el-button>
            </template>
        </el-table-column>
    </el-table>
    <el-pagination
            background
            @current-change="handleCurrentChange"
            layout="prev, pager, next"
            :total="totalCount">
    </el-pagination>
    </div>
    <el-dialog title="添加新菜单" :visible.sync="dialogFormVisible">
        <el-form :model="menuInfo" :rules="rules" ref="menuInfo">
            <el-form-item label="关联编号" prop="mid">
                <el-input v-model="menuInfo.mid"></el-input>
            </el-form-item>
            <el-form-item label="菜单名称" prop="name">
                <el-input v-model="menuInfo.name"></el-input>
            </el-form-item>
            <el-form-item label="请求地址" prop="url">
                <el-input v-model="menuInfo.url"></el-input>
            </el-form-item>
            <el-form-item label="此菜单状态" prop="state">
                <el-select v-model="menuInfo.state" placeholder="请选择菜单状态">
                    <el-option label="0:开启" value="0"></el-option>
                    <el-option label="1:关闭" value="1"></el-option>
                    <el-option label="2:冻结" value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="图标链接" prop="icon">
                <el-input v-model="menuInfo.icon"></el-input>
            </el-form-item>
            <el-form-item label="菜单类型" prop="other">
                <el-input v-model="menuInfo.other"></el-input>
            </el-form-item>
            <el-form-item label="字母类型" prop="type">
                <el-input v-model="menuInfo.type"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addMenu()">添 加</el-button>
        </div>
    </el-dialog>

    <el-dialog title="修改菜单信息" :visible.sync="updatedialogFormVisible">
        <el-form :model="menuInfo" :rules="rules" ref="menuInfo">
            <el-form-item label="关联编号" prop="mid">
                <el-input v-model="menuInfo.mid"></el-input>
            </el-form-item>
            <el-form-item label="菜单名称" prop="name">
                <el-input v-model="menuInfo.name"></el-input>
            </el-form-item>
            <el-form-item label="请求地址" prop="url">
                <el-input v-model="menuInfo.url"></el-input>
            </el-form-item>
            <el-form-item label="此菜单状态" prop="state">
                <el-select v-model="menuInfo.state" placeholder="请选择菜单状态">
                    <el-option label="0:开启" value="0"></el-option>
                    <el-option label="1:关闭" value="1"></el-option>
                    <el-option label="2:冻结" value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="图标链接" prop="icon">
                <el-input v-model="menuInfo.icon"></el-input>
            </el-form-item>
            <el-form-item label="菜单类型" prop="other">
                <el-input v-model="menuInfo.other"></el-input>
            </el-form-item>
            <el-form-item label="字母类型" prop="type">
                <el-input v-model="menuInfo.type"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="updatedialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="updateMenu()">修 改</el-button>
        </div>
    </el-dialog>
</div>
<script>
    var danghui = new Vue({
        el: "#app",
        data: {
            searchInfo: {},
            menuArr: [],
            totalCount: 0,
            menuInfo:{},
            dialogFormVisible:false,
            updatedialogFormVisible:false,
            rules: {
                mid: [
                    {required: true, message: '请输入菜单关联编号', trigger: 'blur'},
                ],
                name: [
                    {required: true, message: '请输入菜单名称', trigger: 'blur'},
                ],
                url: [
                    {required: true, message: '请输入菜单请求地址', trigger: 'blur'},
                ],
                state: [
                    {required: true, message: '请输入此菜单的状态', trigger: 'blur'},
                ],
                icon: [
                    {required: true, message: '请输入图标链接', trigger: 'blur'},
                ],
                other: [
                    {required: true, message: '请输入菜单的类型', trigger: 'blur'},
                ],
                type: [
                    {required: true, message: '请输入字母类型', trigger: 'blur'},
                ],
            }
        },
        methods: {
            updateMenu(){
                danghui.$refs["menuInfo"].validate((valid) => {
                    if (valid) {
                        $.post("/ssm/menu/update", danghui.menuInfo, function (backData) {
                            if (backData.code == 1) {
                                danghui.updatedialogFormVisible = false;
                                danghui.$refs["menuInfo"].resetFields();
                                danghui.$message.success("修改成功");
                                getData(1, 10);
                            } else {
                                danghui.$message.error("修改失败");
                            }
                        });
                    }
                });
            },
            showMenu(id) {
                $.get("/ssm/menu/getMenu/" + id, function (backData) {
                    danghui.menuInfo = backData.data;
                });
                danghui.updatedialogFormVisible = true;
            },

            addMenu(){
                danghui.$refs["menuInfo"].validate((valid) => {
                    if (valid) {
                        $.post("/ssm/menu/add", danghui.menuInfo, function (backData) {
                            if (backData.code == 1) {
                                danghui.dialogFormVisible = false;
                                danghui.$refs["menuInfo"].resetFields();
                                danghui.$message.success("添加成功");
                                getData(1, 10);
                            } else {
                                danghui.$message.error("添加失败");
                            }
                        });
                    }
                });
            },
            showAdd() {
                danghui.dialogFormVisible = true;
            },

            deleteMenu(id) {
                danghui.$confirm('此操作将永久删除该菜单, 是否继续?', '温馨提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    $.get("/ssm/menu/delete/" + id, function (backData) {
                        danghui.$message.success(backData.msg);
                        getData(1, 10)
                    });
                });
            },

            searchMenu() {
                getData(1, 10);
            },
            handleCurrentChange(val) {
                getData(val, 10)
            }
        }
    });

    function getData(a, b) {
        danghui.searchInfo.pageNo = a;
        danghui.searchInfo.pageCount = b;
        $.post("/ssm/menu/list", danghui.searchInfo, function (backData) {
            danghui.menuArr = backData.data.currentData;
            danghui.totalCount = backData.data.totalCount;
        });
    }
    getData(1, 10);

</script>
</body>
</html>